﻿using System;
using System.Windows.Controls;
using System.Windows.Threading;
using CPETwit.WPFClient.Business.TwitterServiceReference;
using CPETwit.WPFClient.Common;
using System.Linq;

namespace CPETwit.WPFClient.UC
{
    /// <summary>
    /// Logique d'interaction pour MainMenu.xaml
    /// </summary>
    public partial class MainMenu : UserControl
    {
        private readonly DispatcherTimer _timer;
        public MainMenu()
        {
            InitializeComponent();

            _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromSeconds(5);
            _timer.Tick += new EventHandler(_timer_Tick);
            _timer.Start();
        }

        private void _timer_Tick(object sender, EventArgs e)
        {
            if (CommonData.LOGGED_USER != null)
            {
                User previousUser = CommonData.LOGGED_USER;
                ServiceManager.RefreshCurrentUser();
                User newUser = CommonData.LOGGED_USER;

                if(previousUser.TweetTwitterProperty.Count() != newUser.TweetTwitterProperty.Count() ||
                    previousUser.AbonnementPublisherProperty.Count() != newUser.AbonnementPublisherProperty.Count() ||
                    previousUser.AbonnementFollowerProperty.Count() != newUser.AbonnementFollowerProperty.Count())
                {
                    DialogManager.UpdateTweetMenus();
                    DialogManager.UpdateMainTimeLine();
                }
            }
        }
    }
}
